/*
* Copyright (c) 2010 The Turing Project
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* Except as contained in this notice, the name(s) of the above
* copyright holders shall not be used in advertising or otherwise to
* promote the sale, use or other dealings in this Software without prior
* written authorization.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
 */

package simulator;

/**
 * This class defines an edge which joins two nodes via a simulated spring.  The
 * nodes are refered two by index values.  An edge's spring action is defined by its
 * resting distance and spring constant.
 * @author David
 * @see simulator.Node
 * @see #defineEdge(int, int, java.lang.Double, java.lang.Double)
 */
public class Edge {
    private Node _nodes[] = new Node[2];
    private Vector3D _edgeVector = new Vector3D();
    private Double _restingDistance;
    private Double _springConstant;
    private Vector3D _edgeForce = new Vector3D();

    /**
     * Constructs a new edge object.
     */
    public Edge() {
        _restingDistance = 40.0;
        _springConstant = 0.001;
    }

    /**
     * Defines an edge as joining two nodes with a spring force defined by its
     * restingDistance and springConstant.
     * @param nodeA
     * @param nodeB
     * @param restingDistance
     * @param springConstant
     */
    public void defineEdge (Node nodeA, Node nodeB, Double springConstant) {

            _nodes[0] = nodeA;
            _nodes[1] = nodeB;
            nodeA.linkTo(nodeB);
            _restingDistance = computeEdgeLength();
            _springConstant = springConstant;

    }

    public Double getNodeCoordinate(int node, int coordinate){
        return _nodes[node].getPositionComponent(coordinate);
    }
        /**
     * Computes the attractive force exhibited by an edge on the two adjoining nodes
     * and applies the force to these nodes.
     * @param edge
     */
    public void applyEdgeForce(Double timeIncrement) {
        computeEdgeForce();
        _nodes[0].applyPositiveForce(_edgeForce, timeIncrement);
        _nodes[1].applyNegativeForce(_edgeForce, timeIncrement);

    }

    public Double computeEdgeLength() {
        _edgeVector.vectorSet(_nodes[0].getVectorPosition());
        _edgeVector.subtractVector(_nodes[1].getVectorPosition());
        return _edgeVector.getAmplitude();
    }
    /**
     * Returns the index value of node 0 or 1.
     * @param node
     * @return
     */


    private void computeEdgeForce() {
        Double amplitude = computeEdgeLength();
        amplitude = (_restingDistance - amplitude) * _springConstant;
        _edgeForce.vectorSet(_edgeVector);
        _edgeForce.normalize();
        _edgeForce.multiplyBy(amplitude);
    }

}
